import pygame
import Graphics
import Text
import Config
import copy
import Vector

smallfont = Text.font(size=int(10 * Config.width/800.0))
fudge = 2

class Wireframe(pygame.sprite.Sprite):
   __slots__ = ('ship',
                'show_ammo',
                'orig_height',
                'orig_width',
                'height',
                'width',
                'weaponicons',
                'wireframe',
                'color',
                'shiprecttl',
                'wdims',
                'highlightedweaponpoint',
                'typing')

   def __init__(self, ship, size, color=(255,255,255), show_ammo=True, pad=(0,0)):
      pygame.sprite.Sprite.__init__(self)
      self.ship = ship
      self.show_ammo = show_ammo
      self.orig_width, self.orig_height = self.ship.image.get_rect().size
      self.width, self.height = size
      self.width += pad[0]
      self.height += pad[1]
      self.image = pygame.Surface((self.width, self.height))
      self.rect = self.image.get_rect()
      self.color = color
      self.wireframe = Graphics.wireframe(Graphics.scale(self.ship.image, Vector.v_sub(size, (2,2)), True), color)
      self.wdims = self.wireframe.get_rect().size
      self.shiprecttl = (self.width/2 - self.wdims[0]/2, self.height/2 - self.wdims[1]/2)
      self.updateweapons()
      self.highlightedweaponpoint = None
      self.update()
      #Hack to fit into UI framework
      self.typing = False

   def drawammocounts(self, ammocounts=None):
      if ammocounts is None:
         ammocounts = self.getammocounts()
      for i, w in enumerate(ammocounts):
         weapon = self.ship.weapons[i]
         if weapon is not None and weapon.ammo > -1:
            ammo = weapon.ammo
            maxammo = weapon.lattr('max_ammo')
            w, h = self.weaponicons[i][2]
            tr = self.weaponicons[i][3]
            maxrect = smallfont.render("%d/%d" % (maxammo, maxammo), 1, (255,255,255)).get_rect()
            maxrect.topright = tr
            pygame.draw.rect(self.image, (0,0,0), maxrect)
            pygame.draw.rect(self.image, (255,255,255), maxrect, 1)
            ammotext = smallfont.render("%d/%d" % (ammo, maxammo), 1, (255,255,255))
            Graphics.blit_anchored(self.image, ammotext, tr, 'topright')
   
   def getammocounts(self):
      ammocounts = [None]*len(self.ship.weapons)
      for i,w in enumerate(self.ship.weapons):
         if w is not None and w.ammo > -1:
            ammocounts[i] = w.ammo
      return ammocounts

   def highlightweaponpoint(self, weaponpoint=None):
      self.highlightedweaponpoint = weaponpoint
   
   def map_pos(self, pt):
      return (self.shiprecttl[0] + int(1.0 * pt[0] * self.wdims[0]), self.shiprecttl[1] + int(1.0 * pt[1] * self.wdims[1]))
      
   def update(self):
      self.image.fill((0,0,0))
      #pygame.draw.rect(self.image, self.color, self.image.get_rect(), 1)
      Graphics.blit_anchored(self.image, self.wireframe,
                             (self.rect.width/2, self.rect.height/2), 'center')
      
      for icon, pos, size, abs_pos in self.weaponicons:
         if icon is not None:
            Graphics.blit_anchored(self.image, icon, pos, 'top')
      
      if self.show_ammo:
         self.drawammocounts()
      if self.ship.shield is not None and self.ship.shield.get_icon() is not None:
         Graphics.blit_anchored(self.image, self.ship.shield.get_icon(), self.map_pos((0.5, 0.45)), 'center')
      if self.ship.generator is not None and self.ship.generator.get_icon() is not None:
         Graphics.blit_anchored(self.image, self.ship.generator.get_icon(), self.map_pos((0.5, 0.75)), 'center')
      if self.highlightedweaponpoint is not None:
         pygame.draw.circle(self.image, (255,255,255), self.weaponicons[self.highlightedweaponpoint][1], 15, 1)

   def updateweapons(self):
      self.weaponicons = [None]*len(self.ship.weapons)
      for i, wp in enumerate(self.ship.weaponpoints):
         weapon = self.ship.weapons[i]
         pos = self.map_pos((1.0 * wp[0] / self.orig_width, 1.0 * wp[1] / self.orig_height))
         if weapon is not None and weapon.get_icon() is not None:
            w,h = weapon.get_icon().get_rect().size
            self.weaponicons[i] = [weapon.get_icon(), pos, (w,h),
                                     Vector.v_add(pos, (w+2, h+2))]
         else:
            self.weaponicons[i] = [None, pos, None, None]
